package com.sky.controller.user;

import com.sky.mapper.DishMapper;
import com.sky.result.Result;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@Slf4j
@Api(tags = "菜品浏览模块")
@RequestMapping("/user/dish")
public class DishController {
    @Autowired
    private DishServiceImpl dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/list")
    @ApiOperation("菜品浏览")
    public Result getBycategoryid(String categoryId){
        //先查redis中有没有对应的缓存数据，若有则从redis中取得数据，否则查询数据库，并将数据库中的数据调入redis缓存中。
        //一个菜品分类缓存一份数据，包含该分类下的所有菜品信息
        String key_in_redis = "DishShowBycategoryId_" + categoryId;//菜品缓存key
        List<DishVO> list = (List<DishVO>)redisTemplate.opsForValue().get(key_in_redis);
        if(list!=null && list.size()>1){
            return Result.success(list);
        }//缓存中有对应categoryId的所有菜品数据
        Integer categoryid = Integer.parseInt(categoryId);
        List<DishVO> result = dishService.getBycategoryid(categoryid);
        redisTemplate.opsForValue().set(key_in_redis,result);
        return Result.success(result);
    }
}
